<template>
  <!-- 订单 -->
  <div class="order-lists main-box">
    <div class="orders-top queryTop">
      <el-input
        placeholder="请输入用户ID"
        v-model.lazy.trim.number="queryInput"
        class="input-with-select"
      >
        <el-button
          @click="queryClick()"
          slot="append"
          icon="el-icon-search"
        ></el-button>
      </el-input>
    </div>

    <el-table
      v-if="orderLists"
      :stripe="true"
      :data="orderLists.goods"
      border
      style="width: 100%"
    >
      <el-table-column align="center" type="index" label="编号" width="50">
      </el-table-column>
      <el-table-column prop="order_number" label="订单编号"> </el-table-column>
      <el-table-column prop="order_price" label="订单价格(元)">
      </el-table-column>
      <el-table-column label="是否付款">
        <template v-slot="{ row }">
          <el-tag v-if="row.pay_status === '1'" type="success">{{
            row.pay_status | getStatus
          }}</el-tag>
          <el-tag v-else type="warning">{{
            row.pay_status | getStatus
          }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="下单时间">
        <template v-slot="{ row }">
          {{ row.create_time | getTime }}
        </template>
      </el-table-column>
      <el-table-column
        prop="is_send"
        width="100"
        align="center"
        label="是否发货"
      >
      </el-table-column>
      <el-table-column label="操作" align="center" width="100">
        <template v-slot="{ row }">
          <el-tooltip
            class="item"
            effect="dark"
            content="编辑订单"
            placement="top-start"
          >
            <el-button
              @click="editOrder(row)"
              size="mini"
              type="primary"
              icon="el-icon-edit"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      v-if="orderLists"
      @size-change="getSize"
      @current-change="getPage"
      :current-page="pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next"
      :total="orderLists.total"
    >
    </el-pagination>

    <div class="tankuang">
      <el-dialog title="修改地址" :visible.sync="dialogFormVisible" size="mini">
        <el-form
          :model="userAdress"
          :rules="rules"
          ref="userAdress"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="省市区/县" prop="city">
            <el-cascader
              class="city"
              collapse-tags
              width="300px"
              :options="cityOptions"
              v-model="userAdress.city"
              @change="changeProvince"
              change-on-select
            >
            </el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="detailedAdress">
            <el-input
              v-model="userAdress.detailedAdress"
              placeholder="请输入详细地址"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="editAdress()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import cityOptions from "../mypub/provinces-china-master/city_data2017_element";
export default {
  data() {
    return {
      orderLists: null,
      pagenum: 1,
      pagesize: 10,
      queryInput: "",
      dialogFormVisible: false,
      cityOptions: cityOptions,

      userAdress: {
        city: "",
        detailedAdress: "",
      },
      rules: {
        city: [
          {
            required: true,
            message: "请输入省市区/县",
            trigger: "blur,change",
          },
        ],
        detailedAdress: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
      },
    };
  },
  filters: {
    getStatus: function (value) {
      if (value === "0") {
        return "未付款";
      } else {
        return "已付款";
      }
    },
  },
  created() {
    this.getOrders();
  },
  methods: {
    async getOrders() {
      const { pagenum, pagesize, queryInput } = this;
      const res = await this.$axios.get(
        `orders?user_id=${queryInput}&&pagenum=${pagenum}&&pagesize=${pagesize}`
      );
      this.orderLists = res.data.data;
      console.log(res);
    },
    getSize(val) {
      this.pagesize = val;
      this.getOrders();
    },
    getPage(val) {
      this.pagenum = val;
      this.getOrders();
    },
    queryClick() {
      this.getOrders();
      console.log(1);
    },
    //订单编辑
    editOrder() {
      this.dialogFormVisible = true;
    },
    editAdress() {
      this.$refs.userAdress.validate(async (valid) => {
        if (valid) {
          this.$message({
            message: "修改订单地址成功",
            type: "success",
          });
          this.userAdress = {
            city: "",
            detailedAdress: "",
          };
          this.dialogFormVisible = false;
        } else {
          console.log("error submit!!");
          // console.log(this.userAdress);
          return false;
        }
      });
    },
    //test
    changeProvince(val) {
      console.log(val);
    },
  },
};
</script>

<style>
.el-scrollbar__wrap {
  height: 300px !important;
}
.city {
  width: 100%;
}
</style>